/**
 * 角色管理的单例
 */
var Menu = {
    id: "menuTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};
/**
 * 操作
 */
Menu.editFormatter=function(value,row,index){
    var btnArr =[];
    var btnUpdate =  {clickEvent:'Menu.openChangeMenu('+row.id+');',title:'修改菜单',icon:HsetIcon['update'],active:false};
    var btnDelete =  {clickEvent:'Menu.delMenu('+row.id+',\''+row.name+'\');',title:'删除菜单',icon:HsetIcon['delete'],active:false};
    if(Authority['update']==true){
        btnUpdate.active=true;
    }
    if(Authority['delete']==true){
        btnDelete.active=true;
    }
    btnArr.push(btnUpdate);
    btnArr.push(btnDelete);
    return HsetIcon.createGroup(btnArr);
};
/**
 * 状态
 * @param value
 * @param row
 * @param index
 * @returns {string}
 */
Menu.statusFormatter=function(value,row,index){
    if(row.status=='1'){
        //开启
        return '<span class="label label-success" >'+value+'</span>';
    }else{
        return '<span class="label label-danger" >'+value+'</span>';
    }
};
/**
 * 初始化表格的列
 */
Menu.initColumn = function () {
    var columns = [
        {title: '序号',align : 'center',valign : 'middle',width:20,radio: false,checkbox:false,formatter:function (value, row, index) {  return index+1; } },
        //{title: 'id', field: 'id', visible: false, width:20,align: 'center', valign: 'middle'},
        {title: '菜单名称', field: 'name', align: 'center',width:200, valign: 'middle', sortable: true},
        {title: '菜单编号', field: 'code', align: 'center',width:100, valign: 'middle', sortable: true},
        {title: '菜单父编号', field: 'pcode', align: 'center',width:100, valign: 'middle', sortable: true},
        {title: '请求地址', field: 'url', align: 'center',width:150, valign: 'middle', sortable: true},
        {title: '排序', field: 'num', align: 'center', width:20,valign: 'middle', sortable: true},
        {title: '层级', field: 'levels', align: 'center',width:20, valign: 'middle', sortable: true},
        {title: '是否是菜单', field: 'isMenuName', align: 'center',width:20, valign: 'middle', sortable: true},
        {title: '状态', field: 'statusName', align: 'center', valign: 'middle', sortable: true,formatter:Menu.statusFormatter},
        {title: '操作', align: 'center', valign: 'middle', width:140, formatter:Menu.editFormatter},
        ]
    return columns;
};

/**
 * 点击添加菜单
 */
Menu.openAddMenu = function () {
    var index = layer.open({
        type: 2,
        title: '添加菜单',
        shade: [0.5,'FFFFFF'],
        offset: '10px',
        area: ['950px', '700px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/menu/menu_add'
    });
    this.layerIndex = index;
};

/**
 * 点击修改
 */
Menu.openChangeMenu = function (id) {
        var index = layer.open({
            type: 2,
            title: '修改菜单',
            shade: [0.5,'FFFFFF'],
            offset: '10px',
            area: ['950px', '700px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/menu/menu_edit/' + id
        });
        this.layerIndex = index;
};

/**
 * 删除
 */
Menu.delMenu = function (id) {
        var operation = function(){
            var ajax = new $ax(Feng.ctxPath + "/menu/remove", function (data) {
                Feng.success("删除成功!");
                Menu.normalSearch();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("menuId", id);
            ajax.setZLoadingIn(true);
        	ajax.setZLoadingOut(true);
            ajax.start();
        };
        Feng.confirm("是否刪除该菜单?", operation);
};
/**
 * 自定义按钮组
 */
Menu.btnGroup=function(){
    var arr=[];
    if(Authority['add']==true){//添加菜单
        arr.push({clickEvent:'Menu.openAddMenu();',title:'添加菜单',icon:HsetIcon['add']});
    }
    return arr;
};



$(function () {
    var defaultColunms = Menu.initColumn();
    var table = new BSTable(Menu.id,"/menu/list", defaultColunms);
    table.setExpandColumn(1);
    table.setPaginationType("client");
    table.setClickToSelect(false);
    table.setCollapseAll(true);//默认是否展开
    table.setIsTreegrid(true);
    table.setPageSize(Number.MAX_VALUE);
    table.setPagination(false);
    table.setBtnGroup(Menu.btnGroup());
    Menu.table = table.init();
    Menu.vs= new $vs(Menu.table).init();
});
